package com.zxy.hotel.controller;


import com.zxy.hotel.entity.Order;
import com.zxy.hotel.entity.RS;
import com.zxy.hotel.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jiusi
 * @since 2021-12-12
 */
@RestController
@RequestMapping("/order")
@PreAuthorize("hasAnyRole('root','admin','staff')")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @PostMapping("/room")
    public RS order(Integer roomId, String name, String cardId, String phone, String remark) {
        if (roomId == null || !StringUtils.hasText(name) || !StringUtils.hasText(cardId) || !StringUtils.hasText(phone)) {
            return RS.fail().message("请输入完整的参数");
        }
        Integer i = orderService.orderRoom(roomId, name, cardId, phone, remark);
        if (i == -1) {
            return RS.fail().message("未找到房间");
        }
        if (i == -2) {
            return RS.fail().message("房间已经有客人了");
        }
        return RS.ok().data(i);
    }

    @PutMapping("/room")
    public RS bill(Integer roomId) {
        if (roomId == null) {
            return RS.fail().message("请输入房间id");
        }
        Order order = orderService.billRoom(roomId);
        return RS.ok().data(order);
    }

    @PostMapping("/roomv")
    public RS bill(Integer roomId, String cardId, String remark) {
        if (roomId == null || !StringUtils.hasText(cardId)) {
            return RS.fail().message("请输入完整参数");
        }
        Integer i = orderService.vipOrderRoom(roomId, cardId, remark);
        if (i == -1) {
            return RS.fail().message("未找到房间");
        }
        if (i == -2) {
            return RS.fail().message("房间已经有客人了");
        }
        if (i == -3) {
            return RS.fail().message("非vip客户");
        }
        return RS.ok().data(i);
    }

    @GetMapping("/stat")
    public RS stat() {
        Map<String, Integer> stat = orderService.getStat();
        return RS.ok().data(stat);
    }
}
